# coding=utf-8

import os

from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager, Shell

from db_recover import create_app, db
from db_recover.Models.models import user, Role, run_mode, basic_sql_command, db_recover_scheme, \
    db_recover_scheme_detail, scheme_attr
from db_recover.db_init import db_init_run

app = create_app(os.environ.get("FLASK_CONFIG") or "default")
manager = Manager(app)
migrate = Migrate(app, db)

from celery import Celery
app.config['CELERY_BROKER_URL'] = 'redis://10.70.61.97:6379/3'
app.config['CELERY_RESULT_BACKEND'] = 'redis://10.70.61.97:6379/4'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

def make_shell_context():  # 创建shell上下文,环境中可以使用这些实例
    return dict(
        app=app,
        db=db,
        user=user,

        Role=Role,
        db_init_run=db_init_run,
        # db_init_user_role=db_init_user_role,
        scheme_attr=scheme_attr,
        run_mode=run_mode,
        basic_sql_command=basic_sql_command,
        db_recover_scheme=db_recover_scheme,
        db_recover_scheme_detail=db_recover_scheme_detail)


manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)

if __name__ == "__main__":
    manager.run()
